//
//  BaseNavViewController.m
//  sklive
//
//  Created by Macx on 2017/5/5.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "BaseNavViewController.h"

#import "LiveListViewController.h"
#import "WaitForInterviewViewController.h"
#import "LiveRoomViewControllerAudience.h"

@interface BaseNavViewController () <UIGestureRecognizerDelegate>

@end

@implementation BaseNavViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	
	self.navigationBar.translucent = NO;
	self.interactivePopGestureRecognizer.delegate = self;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
	if (self.viewControllers.count > 0) {
		
		UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
		[returnBtn setImage:IMAGE(@"return") forState:UIControlStateNormal];
		[returnBtn addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
		returnBtn.frame = CGRectMake(0, 0, 44, 44);
		viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnBtn];
		
		if (@available(iOS 11.0, *)) {
			[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-2.0, -0.4) forBarMetrics:UIBarMetricsDefault];
		} else {
			[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -2) forBarMetrics:UIBarMetricsDefault];
		}
		//第二级则隐藏底部Tab
		viewController.hidesBottomBarWhenPushed = YES;
		
		// 一些页面不需要隐藏BottomBar
		if ([viewController isKindOfClass:[LiveListViewController class]]) {
			viewController.hidesBottomBarWhenPushed = NO;
		}
	}
	[super pushViewController:viewController animated:animated];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
	//  因为聊天界面的语音功能和手势返回功能有冲突，所以这里选择关闭聊天界面的手势返回功能
	if ([self.topViewController isKindOfClass:[ChatViewController class]]||
		[self.topViewController isKindOfClass:[WaitForInterviewViewController class]]||
		[self.topViewController isKindOfClass:[LiveRoomViewControllerAudience class]]) {
		return NO;
	}
	return self.childViewControllers.count > 1;
}

-(void)popAction{
	[self popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
